<template>
  <div>
    <!-- 渲染遍历这个数组 -->
    <div v-for="item in list" :key="item.id">
      <div class="my-goods-item">
        <div class="left">
          <div class="custom-control custom-checkbox">
            <input
              type="checkbox"
              class="custom-control-input"
              id="input"
              :checked="item.goods_state"
              @change="changeFn(item.id)"
            />
            <!-- 上面这个方法是参考之前作业的 自己并不是很会用这个办法   想不出其他的办法 -->
            <label class="custom-control-label" for="input">
              <img :src="item.goods_img" alt="" />
            </label>
          </div>
        </div>
        <div class="right">
          <div class="top">{{ item.goods_name }}</div>
          <div class="bottom">
            <span class="price">¥ {{ item.goods_price }}</span>
            <span>
              <!-- <input type="button" value="-" @click="jianshao(item.id)" /> -->
              <!-- {{ item.goods_count }} -->
              <!-- <input type="button" value="+" @click="add(item.id)" /> -->
              <!-- 使用MyCount -->
              <MyCount
                :id="item.id"
                :goods_count="item.goods_count"
                @sum="sumFn"
                @add="addFn"
                @jianshao="jianshaoFn"
              ></MyCount>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 引入MyCount
import MyCount from '../components/MyCount.vue'

export default {
  components: {
    MyCount,
  },
  methods: {
    changeFn(id) {
      this.$emit('change', id)
    },
    addFn(id) {
      console.log(id)
      const indexs = this.list.findIndex((item) => item.id === id)
      console.log(indexs)
      this.list[indexs].goods_count
      console.log(this.list[indexs].goods_count)
    },
    jianshaoFn(id) {
      console.log(id)
      const indexs = this.list.findIndex((item) => item.id === id)
      if (this.list[indexs].goods_count > 1) {
        this.list[indexs].goods_count
      }
    },
    sumFn(id) {
      console.log(id)
    },
  },
  data() {
    return {
      // Photo: this.goods_img,
    }
  },
  props: {
    list: {
      type: Array,
      requeird: true,
    },
  },
}
</script>

<style lang="less" scoped>
.my-goods-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .left {
    img {
      width: 120px;
      height: 120px;
      margin-right: 8px;
      border-radius: 10px;
    }
    .custom-control-label::before,
    .custom-control-label::after {
      top: 50px;
    }
  }
  .right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .top {
      font-size: 14px;
      font-weight: 700;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      padding: 5px 0;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
}
</style>
